﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

using System.Runtime.InteropServices;
using System.Drawing;
using System.Management;


namespace WiTeach
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            // 修改页面背景主题
           // ImageBrush brush = new ImageBrush();
           // brush.Stretch = Stretch.Fill;
           // brush.ImageSource = new BitmapImage(new Uri("background\\default.jpg", UriKind.Relative));
           // this.Background = brush;
           runWhenstart();//写注册表
            hide_bar();//隐藏任务栏
        }

        ~MainWindow()
        {
            show_bar();//显示任务栏
        }
        public void runWhenstart()
        {
            RegistryKey key = null;
            string exePath = System.Windows.Forms.Application.ExecutablePath;
            key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项                     
            if (key == null)//如果该项不存在的话，则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            if (key.GetValue("wiTeach") == null)
            {
                key.SetValue("wiTeach", exePath);
            }
            key.Close();
        }
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
        /// <summary>
        ///  SetTaskBar  1 表示显示， 0 表示隐藏
        /// </summary>
        /// <param name="istate"></param>
        public void hide_bar()
        {
            IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            IntPtr hStar = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Button", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(trayHwnd, 0);
                ShowWindow(hStar, 0);

            }
        }
        public void show_bar()
        {
            IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            IntPtr hStar = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Button", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(trayHwnd, 9);
                ShowWindow(hStar, 9);

            }
        }
        
        /*
        public void hide_bar()
        {
            IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(trayHwnd, 0);
            }

        }
        public void show_bar()
        {
            IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            if (trayHwnd != IntPtr.Zero)
            {
                ShowWindow(trayHwnd, 9);
            }
        }*/
        /*
        private const int SW_HIDE = 0;  //隐藏任务栏
        private const int SW_RESTORE = 9;//显示任务栏


        [DllImport("user32.dll")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        private void hide_bar()
        {
            ShowWindow(FindWindow("Shell_TrayWnd", null), SW_HIDE);
            //YinYiNiao's Blog
        }

        private void show_bar()
        {

            ShowWindow(FindWindow("Shell_TrayWnd", null), SW_RESTORE);
        }*/
    }
}
